/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package io.iec.edp.caf.context.config;

import io.iec.edp.caf.boot.context.event.FilterInvokeEventBroker;
import io.iec.edp.caf.commons.event.config.EventListenerSettings;
import io.iec.edp.caf.commons.transaction.service.DBTransactionService;
import io.iec.edp.caf.context.filter.CAFContextFilter;
import io.iec.edp.caf.core.context.BizContextManager;
import io.iec.edp.caf.core.context.CafContextServiceImpl;
import io.iec.edp.caf.core.context.ICAFContextService;
import io.iec.edp.caf.data.AuditorAwareImpl;
import io.iec.edp.caf.msu.api.ServiceUnitAwareService;
import io.iec.edp.caf.rest.api.properties.CAFServerConfigurationProperties;
import io.iec.edp.caf.tenancy.api.TenantResolveContributor;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;
import org.springframework.data.domain.AuditorAware;

import java.util.List;

/**
 * This is {@link ApplicationContextConfiguration}.
 *
 * @author yisiqi
 * @since 1.0.0
 */
@Configuration(proxyBeanMethods = false)
@EnableConfigurationProperties({
        CAFServerConfigurationProperties.class
})
public class ApplicationContextConfiguration {

    /**
     * 解决懒加载之后，InitializingBean类型无法正常加载的问题
     * Spring Session data redis 会自动配置事件通知机制，通过InitializingBean来配置的
     * redis-cli config set notify-keyspace-events Egx
     */
    @Bean
    @Lazy(false)
    @ConditionalOnExpression("#{!'false'.equals(environment['redis.enabled'])}")
    @ConditionalOnBean(InitializingBean.class)
    public List<InitializingBean> getAllInitializingBeans(List<InitializingBean> beans) {
        return beans;
    }

    @Bean
    public ICAFContextService cafContextServiceImpl() {
        return new CafContextServiceImpl();
    }

    @Bean
    public FilterInvokeEventBroker filterInvokeEventBroker(EventListenerSettings settings) {
        return new FilterInvokeEventBroker(settings);
    }

    @Bean
    public CAFContextFilter cafContextFilter(BizContextManager contextManager,
                                             FilterInvokeEventBroker filterInvokeEventBroker,
                                             CAFServerConfigurationProperties properties,
                                             ServiceUnitAwareService suAware,
                                             TenantResolveContributor tenantResolveContributor,
                                             DBTransactionService dbTransactionService) {
        return new CAFContextFilter(contextManager, filterInvokeEventBroker, properties, suAware, tenantResolveContributor, dbTransactionService);
    }

    @Bean
    @ConditionalOnProperty("caf-boot.persistence.gsp-cloud.compatibility-mode")
    public AuditorAware<String> cafAuditorAware(ICAFContextService context) {
        return new AuditorAwareImpl(context);
    }

}
